package com.cmol.peopledetectapp.admin;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.cmol.net.Network;
import com.cmol.peopledetectapp.R;
import com.cmol.peopledetectapp.json.User;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

import okhttp3.Call;
import okhttp3.Response;

/**
 * 管理员查看用户
 * 维护用户列表
 *
 * @author 邢文成
 */
public class ListUserActivity extends AppCompatActivity {
    /**
     * 用户列表
     */
    List<User> userList;
    /**
     * 通信模块
     */
    private Network network;
    /**
     * 列表子项
     */
    private List<BeanUser> data = new ArrayList<>();
    /**
     * 传参数
     */
    private Intent intent;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        init();
    }

    /**
     * 初始化函数
     */
    public void init() {
        JSONObject jsonObject = new JSONObject();

        /**
         * 获取用户列表
         */
        network = new Network(jsonObject, "/admin/userList") {
            @Override
            protected void process(Call call, Response response) {
                try {
                    final String body = Objects.requireNonNull(response.body()).string().trim();
                    System.out.println("报文：" + body);
                    JSONObject res = new JSONObject(body);
                    String array = res.getString("userList");
                    Gson gson = new Gson();
                    Type userListType = new TypeToken<ArrayList<User>>() {
                    }.getType();
                    userList = gson.fromJson(array, userListType);
                    System.out.println("获取的用户名：" + userList.get(0).getName());

                    /**
                     * 显示用户列表
                     */
                    for (User user : userList) {
                        System.out.println("接收后的用户名：" + user.getName());
                        BeanUser beanUser = new BeanUser();
                        beanUser.setName(user.getName());
                        data.add(beanUser);
                    }
                } catch (IOException | JSONException e) {
                    e.printStackTrace();
                }
            }

            /**
             * 连接失败
             */
            @Override
            protected void failureWarn() {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Toast.makeText(ListUserActivity.this, "请求用户列表错误", Toast.LENGTH_SHORT).show();
                    }
                });
            }
        };

        try {
            Thread.sleep(100);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        /**
         * 初始化组件
         */
        setContentView(R.layout.activity_list_user);
        intent = new Intent(this, UserManageActivity.class);
        ListView listView = findViewById(R.id.UserList);
        listView.setAdapter(new MyAdapterUser(data, this));

        /**
         * 监听器
         */
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
                // position为表项序号
                intent.putExtra("account", userList.get(position).getName());
                intent.putExtra("password", userList.get(position).getPassword());
//                Log.e("user================", userList.get(position).toString());
                intent.putExtra("id", userList.get(position).getId() + "");
                startActivity(intent);
            }
        });

    }
}